package com.efast.dmom.mgmt.service.mes.impl;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.cafe.portal.entity.common.PortalAttachmentForm;
import com.efast.cafe.portal.remote.common.PortalAttachmentService;
import com.efast.dmom.mgmt.bean.mes.ProductExcuteDto;
import com.efast.dmom.mgmt.dao.mes.TwBasLotDataCollectionDao;
import com.efast.dmom.mgmt.entity.master.TmBasDataCollectionItem;
import com.efast.dmom.mgmt.entity.mes.TrBasProductCfgDci;
import com.efast.dmom.mgmt.entity.mes.TwBasLot;
import com.efast.dmom.mgmt.entity.mes.TwBasLotDataCollection;
import com.efast.dmom.mgmt.service.master.TmBasDataCollectionItemService;
import com.efast.dmom.mgmt.service.mes.TwBasLotDataCollectionService;
import com.efast.dmom.mgmt.utils.Assist;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class TwBasLotDataCollectionServiceImpl implements TwBasLotDataCollectionService{
    @Autowired
    private TwBasLotDataCollectionDao twBasLotDataCollectionDao;
    @Autowired
    private TmBasDataCollectionItemService tmBasDataCollectionItemService;
    
    @Autowired
	private PortalAttachmentService portalAttachmentService;

    @Override
    public long getTwBasLotDataCollectionRowCount(Assist assist){
        return twBasLotDataCollectionDao.getTwBasLotDataCollectionRowCount(assist);
    }
    @Override
    public List<TwBasLotDataCollection> selectTwBasLotDataCollection(Assist assist){
        return twBasLotDataCollectionDao.selectTwBasLotDataCollection(assist);
    }
    @Override
    public TwBasLotDataCollection selectTwBasLotDataCollectionByObj(TwBasLotDataCollection obj){
        return twBasLotDataCollectionDao.selectTwBasLotDataCollectionByObj(obj);
    }
    @Override
    public TwBasLotDataCollection selectTwBasLotDataCollectionById(Integer id){
        return twBasLotDataCollectionDao.selectTwBasLotDataCollectionById(id);
    }
    @Override
    public int insertTwBasLotDataCollection(TwBasLotDataCollection value){
        return twBasLotDataCollectionDao.insertTwBasLotDataCollection(value);
    }
    @Override
    public int insertNonEmptyTwBasLotDataCollection(TwBasLotDataCollection value){
        return twBasLotDataCollectionDao.insertNonEmptyTwBasLotDataCollection(value);
    }
    @Override
    public int insertTwBasLotDataCollectionByBatch(List<TwBasLotDataCollection> value){
        return twBasLotDataCollectionDao.insertTwBasLotDataCollectionByBatch(value);
    }
    @Override
    public int deleteTwBasLotDataCollectionById(Integer id){
        return twBasLotDataCollectionDao.deleteTwBasLotDataCollectionById(id);
    }
    @Override
    public int deleteTwBasLotDataCollection(Assist assist){
        return twBasLotDataCollectionDao.deleteTwBasLotDataCollection(assist);
    }
    @Override
    public int updateTwBasLotDataCollectionById(TwBasLotDataCollection enti){
        return twBasLotDataCollectionDao.updateTwBasLotDataCollectionById(enti);
    }
    @Override
    public int updateTwBasLotDataCollection(TwBasLotDataCollection value, Assist assist){
        return twBasLotDataCollectionDao.updateTwBasLotDataCollection(value,assist);
    }
    @Override
    public int updateNonEmptyTwBasLotDataCollectionById(TwBasLotDataCollection enti){
        return twBasLotDataCollectionDao.updateNonEmptyTwBasLotDataCollectionById(enti);
    }
    @Override
    public int updateNonEmptyTwBasLotDataCollection(TwBasLotDataCollection value, Assist assist){
        return twBasLotDataCollectionDao.updateNonEmptyTwBasLotDataCollection(value,assist);
    }

    public TwBasLotDataCollectionDao getTwBasLotDataCollectionDao() {
        return this.twBasLotDataCollectionDao;
    }

    public void setTwBasLotDataCollectionDao(TwBasLotDataCollectionDao twBasLotDataCollectionDao) {
        this.twBasLotDataCollectionDao = twBasLotDataCollectionDao;
    }
	@Override
	public int saveLotDataCollection(ProductExcuteDto dto) {
		String currentUser = SpringWebUtils.getRequestUser();
		String companyCode = SpringWebUtils.getRequestCompany();
		TwBasLot lot = dto.getRowData();
		List<TwBasLotDataCollection> lotDciList = new ArrayList<TwBasLotDataCollection>();
		String dcItemIds = "";
		for(TrBasProductCfgDci dcItem:dto.getDci()) {
			dcItemIds+=dcItem.getDcItemId()+",";
		}
		if(StringUtils.isNotBlank(dcItemIds)) {
			dcItemIds=dcItemIds.substring(0,dcItemIds.length()-1);
		}
		//获取数据采集项item，返回map
		Map<Integer,Object> itemMap = tmBasDataCollectionItemService.getTmBasDciByIds(dcItemIds);
		for(TrBasProductCfgDci dci:dto.getDci()) {
			TwBasLotDataCollection lotDci = new TwBasLotDataCollection();
			TmBasDataCollectionItem item = (TmBasDataCollectionItem) itemMap.get(dci.getDcItemId());
			lotDci.setProductCfgItemId(dci.getProductCfgItemId());
			lotDci.setStationName(lot.getStationName());
			lotDci.setValueType(item.getValueType());
			lotDci.setComments(item.getComments());
			lotDci.setStationId(lot.getStationId());
			lotDci.setLotName(lot.getLotName());
			lotDci.setProductionLine(lot.getProductionLine());
			lotDci.setDataCollectionItem(dci.getDataCollectionId());
			lotDci.setRoute(lot.getRoute());
			lotDci.setLotId(lot.getLotId());
			lotDci.setOperationId(lot.getOperationId());
			lotDci.setOperation(lot.getOperation());
			lotDci.setValue(dci.getValue());
			lotDci.setStatus(item.getStatus());
			lotDci.setCreateTime(new Date());
			lotDci.setCreateUser(currentUser);
			lotDci.setMarkStatus(dci.getMarkStatus());
			lotDci.setCompanyCode(companyCode);
			lotDci.setOptCounter(dci.getOptCounter());
			lotDci.setGenerateFormId(dci.getGenerateFormId());
			lotDci.setGroupId(dci.getGroupId());
			
			List<PortalAttachmentForm> attachForm = dci.getAttachments();
			if(attachForm != null && attachForm.size() > 0){
				
				portalAttachmentService.saveAttachmentForm(dci.getGenerateFormId(), attachForm);
			}
			
			lotDciList.add(lotDci);
		}
		return twBasLotDataCollectionDao.insertTwBasLotDataCollectionByBatch(lotDciList);
	}
	@Override
	public int updateLotDataCollectionValueByBatch(List<TwBasLotDataCollection> lotDciList, ProductExcuteDto dto) {
		for(TrBasProductCfgDci dci:dto.getDci()) {
			if(lotDciList!=null&&lotDciList.size()>0) {
				for(int i=0;i<lotDciList.size();i++) {
					if(lotDciList.get(i).getProductCfgItemId().equals(dci.getProductCfgItemId())) {
						lotDciList.get(i).setValue(dci.getValue());
						lotDciList.get(i).setGroupId(dci.getGroupId());
						List<PortalAttachmentForm> attachForm = dci.getAttachments();
						if(attachForm != null && attachForm.size() > 0){
							
							portalAttachmentService.saveAttachmentForm(dci.getGenerateFormId(), attachForm);
						}
					}
				}
			}
		}
		return twBasLotDataCollectionDao.updateLotDataCollectionValueByBatch(lotDciList);
	}


}